package edu.cdnu.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import edu.cdnu.common.constant.MessageConstatnt;
import edu.cdnu.common.result.ResultEntity;
import edu.cdnu.pojo.EducationalAdminPojo;
import edu.cdnu.service.EducationalAdminService;
import edu.cdnu.service.StudentService;
import edu.cdnu.service.TeacherService;

/**
 * 管理员操作的接口
 * @author ASUS AsingZ
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

	@Autowired
	private EducationalAdminService educationalAdminService;
	
	@Autowired
	private TeacherService teacherService;
	
	@Autowired
	private StudentService studentService;
	
	/**
	 * 通过角色Id查询用户列表
	 * 1 -- 管理员
	 * 2 -- 教务管理员
	 * 3 -- 教师
	 * 4 -- 班主任
	 * 5 -- 学员
	 * @param roleId
	 * @return
	 */
	@GetMapping("/userList/{roleId}")
	public ResultEntity<?> selectAllUserList(@PathVariable(required=true) Integer roleId){
		switch (roleId) {
		case 1:
			// 这里管理员默认只有一个，查询这个管理员视为参数错误
			return ResultEntity.createFailResult(MessageConstatnt.PARAMETER_EXCEPTION);
		case 2:
			// 2--教务管理员
			return ResultEntity.createSuccessResult(educationalAdminService.selectAllEducationAdminList());
		case 3:
		case 4:
			// 3--教师  4--班主任
			// 教师和班主任一个表，都用这个方法查询
			return ResultEntity.createSuccessResult(teacherService.selectTeacherListByRoleId(roleId));
		case 5:
			// 5--学员
			return ResultEntity.createSuccessResult(studentService.selectAllStudentList());
		case 7:
			// 7-（3+4=7） 把7定义为教师+班主任的所有列表
			return ResultEntity.createSuccessResult(teacherService.selectTeacherListByRoleId(null));
		}
		
		return ResultEntity.createFailResult(MessageConstatnt.DO_IT_FAIL);
	}
	
	/**
	 * 新增或者修改教务管理员
	 * 通过有没得id来判断是新增还是修改
	 * @param educationalAdmin
	 * @return
	 */
	@PostMapping("/addOrUpdateEducationalAdmin")
	public ResultEntity<?> addOrUpdateEducationalAdmin(@RequestBody EducationalAdminPojo educationalAdmin){
		// 通过有没得id来判断是新增还是修改
		if(educationalAdmin.getId()==null) {
			// 没有id是新增
			educationalAdminService.addEducationalAdmin(educationalAdmin);
		}else {
			// 有id是修改
			educationalAdminService.updateEducationalAdmin(educationalAdmin);
		}
		return ResultEntity.createSuccessResult(null);
	}
}
